﻿using MediatR;
using RBAC_Class.Domain;
using RBAC_Class.ErrorCode;
using RBAC_Class.Infrastructrue.Interfaces;
using RBAC_Class.Read.API.Application.Command.Permissions;

namespace RBAC_Class.Read.API.Application.CommandHandler.Permissions
{
    public class GetPermissionCommandHandler:IRequestHandler<GetPermissionCommand,APIResult<List<Permission>>>
    {
        private readonly IBaseRepository<Permission> permissionRep;
        private readonly ILogger<GetPermissionCommandHandler> logger;
        public GetPermissionCommandHandler(IBaseRepository<Permission> permissionRep, ILogger<GetPermissionCommandHandler> logger)
        {
            this.permissionRep = permissionRep;
            this.logger = logger;
        }

        public Task<APIResult<List<Permission>>> Handle(GetPermissionCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var list = permissionRep.GetAll();
                return Task.FromResult(new APIResult<List<Permission>>()
                {
                    Code = ResultCode.Success,
                    Data = list.ToList(),
                    Message = "查询成功"
                });
            }
            catch (Exception ex)
            {
                logger.LogError("查询出错", ex);
                throw;
            }
            
        }
    }
}
